#!/usr/bin/env python
"""
This script will copy the necessary files to get everything
running.
 init will take care of handling the process, and
everything else will be placed in /etc/webstatus
"""

import os.path
from subprocess import Popen
import optparse

main_directory = "/etc/webstatus"
init = "/etc/init.d/webstatus"
log = "/var/log/webstatus"


def check_files():
	"""Check for files, if they are not there
	create them."""	

	if os.path.isdir(main_directory):
		print "Main directory already in /etc/"
	else:
		Popen("mkdir /etc/webstatus", shell=True)
		Popen("cp * /etc/webstatus/", shell=True)
		# Then remove webstatus the script as we don't need that
		Popen("rm -f /etc/webstatus/webstatus", shell=True)
		print "Created /etc/webstatus and"
		print "copied necessary files."

	if os.path.isfile(init):
		print "Init file already installed."
	else:
		Popen("cp webstatus /etc/init.d/", shell=True)
		print "Copied webstatus to init.d"

	if os.path.isdir(log):
		print "Log directory already installed."
	else:
		Popen("mkdir /var/log/webstatus", shell=True)
		print "Created the log directory in /var/log/webstatus."

def remove():
	if os.path.isdir(main_directory):
		Popen("rm -r /etc/webstatus", shell=True)
		print "Removed /etc/webstatus"
	else:
		print "The /etc/webstatus directory was already removed."

	if os.path.isfile(init):
		Popen("rm -r /etc/init.d/webstatus", shell=True)
		print "Removed /etc/init.d/webstatus"
	else:
		print "The init file for webstatus was already removed."

	if os.path.isdir(log):
		Popen("rm -r /var/log/webstatus", shell=True)
		print "Removed /var/log/webstatus."
	else:
		print "The log file directory was already removed."

def main():
	p = optparse.OptionParser(description="Install/Remove webstatus.",
							prog="setup.py",
							version="0.1",
							usage="%prog [install/remove]")
	options, args = p.parse_args()

	if len(args) == 1:
		if args[0] == "install":
			check_files()

		if args[0] == "remove":
			remove()

	else:
		p.print_help()

if __name__ == '__main__':
	main()
